import { We_getInstance } from '@/we-api/we-env'
import { We_LayerEnum } from '@/we-api/we-layer/enum-legend'

const We_updateFilter = (filterMin, filterMax, layerName: string, _WE = null) => {
  const WE = _WE || We_getInstance()
  if (!WE) return
  const layer = WE.layerManager.getById(layerName)
  if (!layer) return
  layer.display.filterMin = filterMin
  layer.display.filterMax = filterMax
  return layer
}

const We_updateLegend = (layerName: string, _WE = null) => {
  const WE = _WE || We_getInstance()
  if (!WE) return
  const layer = WE.layerManager.getById(layerName)
  if (!layer) return
  layer.display.weDisplayLegend.dirty = true
  layer.display.dirty = true
}

// 格点
export const viewModel_GridColorAndValueRamp = ref<any>(null)
export const onGridLayerFilter = ([filterMin, filterMax]) => {
  We_updateFilter(filterMin, filterMax, We_LayerEnum.FeatureGrid)
}
export const onGridLayerColorChange = () => {
  We_updateLegend(We_LayerEnum.FeatureGrid)
}

// 风场
export const viewModel_WindColorAndValueRamp = ref<any>(null)
export const onWindLayerFilter = ([filterMin, filterMax]) => {
  We_updateFilter(filterMin, filterMax, We_LayerEnum.FeatureWind)
}
export const onWindLayerColorChange = () => {
  We_updateLegend(We_LayerEnum.FeatureWind)
}

// 三维体
export const viewModel_VolumeColorAndValueRamp = ref<any>(null)
export const onVolumeLayerFilter = ([filterMin, filterMax]) => {
  const WE = We_getInstance()
  if (!WE) return
  const layer = WE.layerManager.getById(We_LayerEnum.FeatureVolume)
  if (!layer) return

  if (!layer.hasDisplayAttachObj) {
    We_updateFilter(filterMin, filterMax, We_LayerEnum.FeatureVolume)
  }

  WeatherEarth.WeDisplayParameter.Volume.filterMin = filterMin
  WeatherEarth.WeDisplayParameter.Volume.filterMax = filterMax
  WeatherEarth.WeDisplayParameter.Lengthways.filterMin = filterMin
  WeatherEarth.WeDisplayParameter.Lengthways.filterMax = filterMax
}
export const onVolumeLayerColorChange = () => {
  We_updateLegend(We_LayerEnum.FeatureVolume)
}

// 等值面
export const viewModel_IsoSurfaceColorAndValueRamp = ref<any>(null)
